const app = getApp();
function request(userInfo, url, methods, data, success) {
	let appurl = app.globalData.baseurl + url;
	let contenttype = '';
	if (methods == 'post') {
		contenttype = 'application/x-www-form-urlencoded;charset=utf-8';
	} else {
		contenttype = 'application/json';
	}
	wx.showLoading({
		title: '客官不要急~',
		mask: true
	})
	wx.request({
		url: appurl,
		header: {
			'content-type': contenttype,
			'headerId': userInfo.openId,
			'headerValue': userInfo.headerValue
		},
		method: methods,
		data: data,
		success(res) {
			if (res.data.status == 200) {
				wx.hideLoading(); /* 隐藏加载框 */
				success(res);
			} else if(res.data.status == 500){
				wx.hideLoading(); 
				wx.showModal({
					title: "提示",
					content: res.data.msg,
					showCancel: false
				});
			}else{
				wx.hideLoading(); 
				success(res);
			}
		},
		fail(res){
			console.log('请检查是否有网络');
			wx.hideLoading(); 
			wx.showLoading({
				title: '请检查网络重启~',
				mask: true
			})
			
		}
	});
};

function _login(_success) {
	let that = this;
	//let loginnum=num;
	// 登录
	wx.login({
		success: res => {
			if(res.errMsg=='login:ok'){
				let usercode = res.code;
				wx.setStorageSync('usercode', usercode.code);
				let base_url = app.globalData.baseurl;
				let _url = base_url + '/base/login';
				wx.request({
					url: _url,
					data: {
						code: usercode
					},
					method: 'post',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					success: function(res) {
						if (res.data.status == 200) {
							_success(res);
						}
					},
				})
			}},
		fail: res => {
			wx.hideLoading(); 
			console.log(3);
			wx.showModal({
				title: "提示",
				content: "微信登录失败。",
				showCancel: false
			});
		}
	});
}

/* 判断是否有滚动条 */
function scrolllefticon(cate_nav_list,domeone,dometwo,success,fail){
	let that=this;
	let length=cate_nav_list.length;
	let listheight='';
	//创建节点选择器
	var query = wx.createSelectorQuery();
	//选择id
	query.select(domeone).boundingClientRect()
	query.exec(function (res) {
	  //res就是 所有标签为mjltest的元素的信息 的数组
	  //取高度
	  listheight=res[0].height;
	})
	var querytwo = wx.createSelectorQuery();
	//选择id
	querytwo.select(dometwo).boundingClientRect()
	querytwo.exec(function (res) {
	  //res就是 所有标签为mjltest的元素的信息 的数组
	  //取高度
	  let listnavheight=Number(res[0].height)*length;
	  if(listnavheight>listheight){
		  setTimeout(function(){
			  success(res);
		  },1000)
	  }else{
		  fail(res);
	  }
	})
	
	
}

module.exports = {
	request: request,
	_login:_login,
	scrolllefticon:scrolllefticon
}
